;
(function () {
  new Vue({
    el: '#business-config',
    data: {
      channelTypeList: [],
      channelNameList: [],
      allChannelNameList: [],
      isShowChannelTypeIpt: false,
      isShowChannelNameIpt: false,
      addTypeValue: null,
      addNameValue: null,
      parentCode: null,
      tmpName: null, // 用于编辑时缓存名称

      active: false, //启用禁用中介
      commissionObj: '', //佣金对象
      typeList: [{
        name: '公司',
        code: 'C'
      }, {
        name: '个人',
        code: 'P'
      }], //中介类型
      configType: '', //渠道类型的配置类型	
      defaultFlag: '', //渠道类型的是否默认
      channeIndex: null,
      addEditDefaultFlag: false, //新增、编辑默认渠道名称flag
      addEditDefaultType: '', //渠道名称新增或者编辑
      editChanneCode: '', //编辑渠道名称的code
      channelItem: {
        dictName: '',
        intermediaryType: [],
        intermediaryIdCode: '',
        intermediaryName: '',
        intermediaryPhone: '',
        intermediaryAddress: '',
      },

    },
    directives: {
      focus: {
        inserted: function (el) {
          el.focus()
        },
        update: function (el) {
          el.focus()
        }
      }
    },
    methods: {
      // 获取是否在合同中体现中介佣金
      getCommission() {
        hlhttp.get({
          url: 'configure/dict',
          data: {
            'module_code': 'ZHSH',
            'config_name': 'ZS_CHANNEL_INTERMEDIARY'
          },
          success: res => {
            this.commissionObj = res[0]
          }
        })
      },
      // 中介启用禁用开关
      switchActive(val, type) {
        let url = val ? 'configure/merchant_process/_enable' : 'configure/merchant_process/_disable'
        if (type == 'zj') {
          this.intermediaryActive(val,url)
        }
        if (type == 'yj') {
          this.commissionActive(val,url)
        }
      },
      // 中介公司的启用禁用方法
      intermediaryActive(val,url) {
        hlhttp.put({
          url: url,
          data: {
            'module_code': 'ZHSH',
            'config_name': 'ZS_CHANNEL_CATE',
            'dict_code': this.channelTypeList[0].dictCode,
          },
          success: res => {
            this.$set(this.channelTypeList[0], 'status', val ? 'Y' : 'N')
            if (!val) {
              this.commissionActive(val,'configure/merchant_process/_disable')
            }
          }
        })
      },
      // 佣金的启用禁用
      commissionActive(val,url) {
        hlhttp.put({
          url: url,
          data: {
            'module_code': 'ZHSH',
            'config_name': 'ZS_CHANNEL_INTERMEDIARY',
            'dict_code': this.commissionObj.dictCode,
          },
          success: res => {
            this.$set(this.commissionObj, 'status', val ? 'Y' : 'N')
          }
        })
      },



      // 关闭新增编辑渠道弹出层
      closePop() {
        this.addEditDefaultFlag = false
      },
      // 校验渠道名称是否填写完成
      checkSubmit() {
        if (!this.channelItem.dictName) {
          hl.toast('请输入渠道名称')
          return false
        }
        if (!this.channelItem.intermediaryName) {
          hl.toast('请输入转介人')
          return false
        }
        if (!this.channelItem.intermediaryPhone) {
          hl.toast('请输入联系方式')
          return false
        }
        if (!this.channelItem.intermediaryAddress) {
          hl.toast('请输入通讯地址')
          return false
        }
        if (this.channelItem.intermediaryType + '' == 'P') {
          if (!this.channelItem.intermediaryIdCode) {
            hl.toast('请输入身份证号')
            return false
          }
        }
        return true
      },
      confirmSubmit() {
        if (!this.checkSubmit()) return
        if (this.addEditDefaultType == 'add') {
          let data = {
            module_code: 'ZHSH',
            config_name: 'ZS_CHANNEL_NAME',
            config_type: this.configType,
            parent_code: this.parentCode,
            default_flag: 'N',
            dict_name: this.channelItem.dictName,
            intermediary_type: this.channelItem.intermediaryType + '',
            intermediary_id_code: '#',
            intermediary_name: this.channelItem.intermediaryName,
            intermediary_phone: this.channelItem.intermediaryPhone,
            intermediary_address: this.channelItem.intermediaryAddress,
          }
          if (this.channelItem.intermediaryType + '' == 'P') {
            data.intermediary_id_code = this.channelItem.intermediaryIdCode
          }
          hlhttp.post({
            url: 'configure/saveIntermediaryInfo',
            data: data,
            success: res => {
              hl.toast('新增成功')
              this.getChannelName('select')
              this.addEditDefaultFlag = false
            }
          })
        } else {
          let data = {
            module_code: 'ZHSH',
            config_name: 'ZS_CHANNEL_NAME',
            dict_name: this.channelItem.dictName,
            dict_code: this.editChanneCode,
            intermediary_type: this.channelItem.intermediaryType + '',
            intermediary_id_code: '#',
            intermediary_name: this.channelItem.intermediaryName,
            intermediary_phone: this.channelItem.intermediaryPhone,
            intermediary_address: this.channelItem.intermediaryAddress,
          }
          if (this.channelItem.intermediaryType + '' == 'P') {
            data.intermediary_id_code = this.channelItem.intermediaryIdCode
          }
          hlhttp.put({
            url: 'configure/updateIntermediaryInfoByDictCode',
            data: data,
            success: res => {
              hl.toast('编辑成功')
              this.getChannelName('select')
              this.addEditDefaultFlag = false
            }
          })
        }
      },
      getChannelType() {
        var self = this;
        hlhttp.get({
          url: 'configure/dict',
          data: {
            'module_code': 'ZHSH',
            'config_name': 'ZS_CHANNEL_CATE'
          },
          success: data => {
            data[0].defaultFlag = 'Y'
            data.forEach(el => {
              el['isEdit'] = 'N'
              el['isSelect'] = 'N'
            })
            self.channelTypeList = data;
          }
        })
      },
      getChannelName(val) {
        var self = this;
        hlhttp.get({
          url: 'configure/dict',
          data: {
            'module_code': 'ZHSH',
            'config_name': 'ZS_CHANNEL_NAME'
          },
          success: data => {
            data.forEach(el => el['isEdit'] = 'N')
            self.allChannelNameList = data;
            if (val) {
              self.selectChannelType(this.channelTypeList[0].dictName, this.channelTypeList[0].dictCode, 0, this.channelTypeList[0].configType, this.channelTypeList[0].defaultFlag)
            }
          }
        })
      },
      selectChannelType(name, code, i, configType, defaultFlag) {
        this.defaultFlag = defaultFlag
        this.channeIndex = i
        this.configType = configType
        this.parentCode = code;
        this.channelNameList = this.allChannelNameList.filter(el => el.parentDictCode == code);
        this.channelTypeList.forEach(el => el['isSelect'] = 'N')
        this.$set(this.channelTypeList[i], 'isSelect', 'Y')
      },
      addType() {
        this.addTypeValue = null;
        this.isShowChannelTypeIpt = true;
      },
      addChannel(type) {
        if (this.channeIndex == null) {
          hl.toast('请选择渠道分类');
          return
        }
        this.addEditDefaultType = type
        if (this.channeIndex == '0') {
          this.channelItem = {
            dictName: '',
            intermediaryType: ['C'],
            idCard: '',
            intermediaryName: '',
            phone: '',
            address: '',
          }
          this.addEditDefaultFlag = true
          return
        }
        this.addNameValue = null;
        this.isShowChannelNameIpt = true;
      },
      editChannelType(i) {
        this.tmpName = this.channelTypeList[i]['dictName']
        this.$set(this.channelTypeList[i], 'isEdit', 'Y')
      },
      delChannelType(code, i) {
        var self = this;
        hlhttp.delete({
          url: 'configure/merchant_channel?module_code=ZHSH&&config_name=ZS_CHANNEL_CATE&&dict_code=' + code,
          success: data => {
            self.channelTypeList.splice(i, 1);
            hl.toast('删除成功')
          }
        })
      },
      editChannelName(i, item, type) {
        if (this.channeIndex == '0') {
          this.addEditDefaultFlag = true
          this.addEditDefaultType = type
          this.editChanneCode = item.dictCode
          hlhttp.get({
            url: 'configure/getIntermediaryInfoByDictCode',
            data: {
              dict_code: item.dictCode
            },
            success: res => {
              this.channelItem = {
                dictName: item.dictName,
                intermediaryType: [res.intermediaryType],
                intermediaryIdCode: res.intermediaryIdCode || '',
                intermediaryName: res.intermediaryName,
                intermediaryPhone: res.intermediaryPhone,
                intermediaryAddress: res.intermediaryAddress,
              }
            }
          })
          return
        }
        this.$set(this.channelNameList[i], 'isEdit', 'Y')
      },
      delChannelName(code, i) {
        if (this.channeIndex == '0') {
          hlhttp.delete({
            url: 'configure/deleteIntermediaryInfoByDictCode?module_code=ZHSH&&config_name=ZS_CHANNEL_NAME&&dict_code=' + code,
            success: res => {
              this.getChannelName()
              this.channelNameList.splice(i, 1);
              hl.toast('删除成功')
            }
          })
          return
        }
        hlhttp.delete({
          url: 'configure/merchant_channel?module_code=ZHSH&&config_name=ZS_CHANNEL_NAME&&dict_code=' + code,
          success: res => {
            this.getChannelName()
            this.channelNameList.splice(i, 1);
            hl.toast('删除成功')
          }
        })
      },
      hideChannelTypeIpt() {
        this.isShowChannelTypeIpt = false;
      },
      hideChannelNameIpt() {
        this.isShowChannelNameIpt = false;
      },
      confirmAddChannelType() {
        var self = this;
        let reg = /^[a-zA-Z0-9_\u4e00-\u9fa5]+$/;
        if (!reg.test(this.addTypeValue)) {
          hl.toast('内容格式只支持只支持中文、英文、数字、下划线');
          return
        }
        if (!self.addTypeValue) {
          hl.toast('渠道分类不能为空');
          return
        }

        hlhttp.post({
          url: 'configure/merchant_channel',
          data: {
            'module_code': 'ZHSH',
            'config_name': 'ZS_CHANNEL_CATE',
            'parent_code': '#',
            'value': self.addTypeValue
          },
          success: data => {
            self.channelTypeList.push({
              'dictCode': data.dictCode,
              'dictName': self.addTypeValue,
              'isEdit': 'N',
              'isSelect': 'N'
            })
            this.selectChannelType(self.addTypeValue, data.dictCode, self.channelTypeList.length - 1)
            self.isShowChannelTypeIpt = false;
            hl.toast('新增成功')
          }
        })
      },
      cancelAddChannelType() {
        this.isShowChannelTypeIpt = false;
      },
      confirmAddChannelName() {
        var self = this;
        if (!self.addNameValue) {
          hl.toast('渠道名称不能为空');
          return
        }

        hlhttp.post({
          url: 'configure/merchant_channel',
          data: {
            'module_code': 'ZHSH',
            'config_name': 'ZS_CHANNEL_NAME',
            'parent_code': self.parentCode,
            'value': self.addNameValue
          },
          success: data => {
            self.getChannelName()
            self.channelNameList.push({
              'dictCode': data.dictCode,
              'dictName': self.addNameValue,
              'parentDictCode': self.parentCode,
              'isEdit': 'N'
            })
            self.isShowChannelNameIpt = false;
            hl.toast('新增成功')
          }
        })
      },
      cancelAddChannelName() {
        this.isShowChannelNameIpt = false;
      },
      confirmEditChannelType(name, code, i) {
        var self = this;
        let reg = /^[a-zA-Z0-9_\u4e00-\u9fa5]+$/;
        if (!reg.test(name)) {
          hl.toast('内容格式只支持只支持中文、英文、数字、下划线');
          return
        }
        hlhttp.put({
          url: 'configure/merchant_channel',
          data: {
            'module_code': 'ZHSH',
            'config_name': 'ZS_CHANNEL_CATE',
            'dict_code': code,
            'value': name
          },
          success: data => {
            self.$set(this.channelTypeList[i], 'isEdit', 'N')
            hl.toast('编辑成功')
          }
        })
      },
      cancelEditChannelType(i) {
        this.$set(this.channelTypeList[i], 'dictName', this.tmpName)
        this.$set(this.channelTypeList[i], 'isEdit', 'N')
      },
      confirmEditChannelName(name, code, i) {
        var self = this;
        hlhttp.put({
          url: 'configure/merchant_channel',
          data: {
            'module_code': 'ZHSH',
            'config_name': 'ZS_CHANNEL_NAME',
            'dict_code': code,
            'value': name
          },
          success: data => {
            self.$set(self.channelNameList[i], 'isEdit', 'N')
            hl.toast('编辑成功')
          }
        })
      },
      cancelEditChannelName(i) {
        this.$set(this.channelNameList[i], 'isEdit', 'N')
      }
    },
    created() {
      this.getChannelType()
      this.getChannelName()
      this.getCommission()
    },
    mounted() {

    }
  })

}())